package com.tju.backend.utils.tool;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串过滤工具类
 *
 * @USER: CLS
 */
@Slf4j
public final class StringUtil {

    /**
     * 过滤字符
     *
     * @param source
     * @param replace
     * @return
     */
    public static String replaceWrongUnicode(String source, String replace) {
        if (StringUtils.isBlank(source)) {
            return source;
        }
        if (StringUtils.isBlank(replace)) {
            replace = "";
        }
        Pattern CRLF = Pattern.compile("([&#xa0;➤▶★■◆■◆♧♡◓◒♠♣♥❤☜☞☎☏⊙◎◚◛◜▧▨♨◐◑↔↕▪ ▒ ◊◦▣▤▥▦▩◘◈◇♬♪♩♭♪の◐◑→あぃ￡Ю〒§♤♥▶¤๑⊹⊱⋛⋌⋚⊰⊹≈๑۩۩.. ..۩۩๑๑۩۞۩๑✱❇✾➹ ~.~‿☀☂☁┱┲❣✚✩✣✤✥✦❈❥❦❧❂❁❀✿✄☪☣☢☠☭ღღღ▶▷◀◁☀☁☂☃☄◐◑☇☈⊙☊☋☌☍ⓛⓞⓥⓔ╬『』∴☀ . ｡◕‿◕｡♨♬♩♭♧◑∷﹌の◐◎▶☺◛►◄▧▨♨◐◑ ↔ ↕↘◜▀▄█▌░▒▬♦◊ ☜☞▐░▒▬♦◊◦ ◜♧の◑→♧ぃ￡❤｡◕‿◕｡✌✟ஐ♧♬๑•ิ.•ิ๑♠♣✖♥►◄↔↕▪▫◘◙の◑→あぃ￡❤｡◕‿�▲×●]|(&hellip;)|(&middot;)|(&nbsp;)|[\\u0000-\\u0019]|[\\u001A-\\u001F]|[\\u001a-\\u001f]|[\\u007f-\\u009f]|\\u00ad|[\\u0483-\\u0489]|[\\u0559-\\u055a]|\\u058a|[\\u0591-\\u05bd]|\\u05bf|[\\u05c1-\\u05c2]|[\\u05c4-\\u05c7]|[\\u0606-\\u060a]|[\\u063b-\\u063f]|\\u0674|[\\u06e5-\\u06e6]|\\u070f|[\\u076e-\\u077f]|\\u0a51|\\u0a75|\\u0b44|[\\u0b62-\\u0b63]|[\\u0c62-\\u0c63]|[\\u0ce2-\\u0ce3]|[\\u0d62-\\u0d63]|\\u135f|[\\u200b-\\u200f]|[\\u2028-\\u202e]|\\u2044|\\u2071|[\\uf701-\\uf70e]|[\\uf710-\\uf71a]|\\ufb1e|[\\ufc5e-\\ufc62]|\\ufeff|\\ufffc)");
        Matcher m = CRLF.matcher(source);
        if (m.find()) {
            return m.replaceAll(replace);
        }
        return source;
    }


    private StringUtil() {
    }

    private static final Set<String> notAllowedKeyWords = new HashSet<>(0);

    static {
        String key = "and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+";
        String[] keyStr = key.split("\\|");
        notAllowedKeyWords.addAll(Arrays.asList(keyStr));
    }

    /**
     * 防xss攻击
     */
    public static String cleanXSS(String valueBefore) {
        String valueAfter = valueBefore.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
        valueAfter = valueAfter.replaceAll("<", "& lt;").replaceAll(">", "& gt;");
        valueAfter = valueAfter.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");
        valueAfter = valueAfter.replaceAll("'", "& #39;");
        valueAfter = valueAfter.replaceAll("eval\\((.*)\\)", "");
        valueAfter = valueAfter.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
        valueAfter = valueAfter.replaceAll("script", "");
        return valueAfter;
    }
}

